package top.drinyeit.weilong.presenter;

import android.support.v4.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.fragment.MineFragment;
import top.drinyeit.weilong.model.UserInfBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

/**
 * Created by bigHead on 2018/1/3.
 */

public class MinePresenter {
    private MineFragment mineView;

    public MinePresenter(MineFragment mineView) {
        this.mineView = mineView;
    }

    //获取用户个人信息
    public void getInf(String user_id) {
        if (SPUtil.getShareString(Constant.USER_ID).equals("")) {
            return;
        }
        mineView.showLoading();
        CommonSubscriber<HttpResponse<UserInfBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<UserInfBean>>() {
            @Override
            public void onError(String e, int code) {
                mineView.dismissLoading();
                mineView.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<UserInfBean> o) {
                mineView.dismissLoading();
                if (o.getCode() == 1) {
                    Glide.with(mineView)
                            .load(o.getData().getHead_photo_url())
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .dontTransform()
                            .into(mineView.head2);
                    mineView.userName.setText(o.getData().getNick_name());
//                    mineView.level
                    changeLv(o.getData().getMember_level());
                } else {
                    mineView.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getInf(subscriber, user_id);
    }

    //显示等级
    private void changeLv(int level) {
        switch (level) {
            default:
                mineView.level.setImageDrawable(ContextCompat.getDrawable(mineView.getActivity(), R.drawable.lv_icon));
                break;
        }
    }
}
